package design.simdi.code.final_paper.site.panels.licente.candidati;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.injection.web.InjectorHolder;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.hibernate.criterion.Criterion;

import design.simdi.code.final_paper.core.daos.StudentCandidatDao;
import design.simdi.code.final_paper.core.entities.Person;
import design.simdi.code.final_paper.core.entities.Sesiune;
import design.simdi.code.final_paper.core.entities.StudentCandidat;
import design.simdi.code.final_paper.core.services.ApplicationService;

/**
 * 
 * @author Marius Dumitriu - (Jun 25, 2010 - 1:39:53 AM)
 * 
 */
public class SortableCandidatDataProvider extends SortableDataProvider<StudentCandidat> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@SpringBean
	private StudentCandidatDao studentCandidatDao;

	@SpringBean
	private ApplicationService applicationService;

	private List<Criterion> criterii;
	private Person owner;
	private Sesiune sesiune;

	/**
	 * constructor
	 */
	public SortableCandidatDataProvider() {
		InjectorHolder.getInjector().inject(this);
		// set default sort
		setSort("status", true);
		criterii = new ArrayList<Criterion>();
	}

	/**
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#iterator(int,
	 *      int)
	 */
	public Iterator<StudentCandidat> iterator(int first, int count) {
		// SortParam sp = getSort();
		// Order o;
		// if (sp.isAscending())
		// o = Order.asc(sp.getProperty());
		// else
		// o = Order.desc(sp.getProperty());

		String query;
		if (getOwner() != null && getOwner().getProfesor() != null)
			query = String
					.format(
							"select distinct sc from StudentCandidat as sc, Domain as d, Project as pr , Person as p, Sesiune as s where "
									+ "(sc.domain = d  and d.author = p and p.id = '%s' and d.sesiune = s and s.id= '%s') or "
									+ "(sc.project = pr  and pr.author = p and p.id = '%s' and pr.sesiune = s and s.id= '%s') ",
							getOwner().getId(), getSesiune().getId(), getOwner().getId(), getSesiune().getId());
		else
			query = String.format(
					"select distinct sc from StudentCandidat as sc, Domain as d , Project as pr, Sesiune as s where "
							+ "(sc.domain = d  and d.sesiune = s and s.id= '%s' ) or "
							+ "(sc.project = pr  and pr.sesiune = s and s.id= '%s' )", getSesiune().getId(),
					getSesiune().getId());
		List<StudentCandidat> lista = studentCandidatDao.find(query);

		return lista.subList(first, first + count).iterator();

	}

	/**
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#size()
	 */
	public int size() {
		String query;
		if (getOwner() != null && getOwner().getProfesor() != null)
			query = String
					.format(
							"select distinct sc from StudentCandidat as sc, Domain as d, Project as pr , Person as p, Sesiune as s where "
									+ "(sc.domain = d  and d.author = p and p.id = '%s' and d.sesiune = s and s.id= '%s') or "
									+ "(sc.project = pr  and pr.author = p and p.id = '%s' and pr.sesiune = s and s.id= '%s') ",
							getOwner().getId(), getSesiune().getId(), getOwner().getId(), getSesiune().getId());
		else
			query = String.format(
					"select distinct sc from StudentCandidat as sc, Domain as d , Project as pr, Sesiune as s where "
							+ "(sc.domain = d  and d.sesiune = s and s.id= '%s' ) or "
							+ "(sc.project = pr  and pr.sesiune = s and s.id= '%s' )", getSesiune().getId(),
					getSesiune().getId());
		return studentCandidatDao.find(query).size();

	}

	/**
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#model(java.lang.Object)
	 */
	public IModel<StudentCandidat> model(StudentCandidat object) {
		return new DetachableCandidatModel(object, studentCandidatDao);
	}

	public void setCriterii(List<Criterion> criterii) {
		this.criterii = criterii;
	}

	public List<Criterion> getCriterii() {
		return criterii;
	}

	public void setStudentCandidatDao(StudentCandidatDao studentCandidatDao) {
		this.studentCandidatDao = studentCandidatDao;
	}

	public StudentCandidatDao getStudentCandidatDao() {
		return studentCandidatDao;
	}

	public void setApplicationService(ApplicationService applicationService) {
		this.applicationService = applicationService;
	}

	public ApplicationService getApplicationService() {
		return applicationService;
	}

	public void setOwner(Person owner) {
		this.owner = owner;
	}

	public Person getOwner() {
		return owner;
	}

	public void setSesiune(Sesiune sesiune) {
		this.sesiune = sesiune;
	}

	public Sesiune getSesiune() {
		return sesiune;
	}

}